package com.wavehi.homework.first;

import com.wavehi.pojo.Person;

import java.lang.reflect.Constructor;

/**
 * @Author WaveHi
 * @Date 25/8/2021 下午 7:07
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description First homework:Get constructors.
 */
public class ReflectionDemo1 {

    public static void main(String[] args) throws NoSuchMethodException {

        //获取Class对象
        Class<Person> personClass = Person.class;

        //获取所有的公开的构造器
        Constructor<?>[] constructors = personClass.getConstructors();
        System.out.println("Public constructors:");
        for(Constructor constructor : constructors) {

            System.out.println(constructor);

        }

        //获取所有的构造器
        Constructor<?>[] declaredConstructors = personClass.getDeclaredConstructors();
        System.out.println("Declared constructors :");
        for(Constructor declaredConstructor : declaredConstructors) {

            System.out.println(declaredConstructor);

        }

        //获取无参构造器
        System.out.println("The public constructor where there no parameter is :" + personClass.getConstructor());

        //获取有参构造器
        System.out.println("The public constructor where there are parameters is :" + personClass.getConstructor(String.class, int.class));

        //获取私有指定的构造器
        System.out.println("The constructor where there are parameters is:" + personClass.getDeclaredConstructor(String.class));

    }

}

//小结：Constructor<T> getConstructor(Class<?>... parameterTypes) 获取指定公共的构造器
//     Constructor<?>[] getConstructors() 获取由公共构造器组成的数组
//     Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) 获取指定的构造器
//     Constructor<?>[] getDeclaredConstructors() 获取由所有构造器组成的数组